import request from "/utils/request"
import { getMerchantList } from "/api/clock/index"
const Promise = require('/plugins/es6-promise')

Page({
  data: {
    status: 1,
    pageSize: 10,
    search: "",
    private: {
      total: 0,
      list: []
    },
    signed: {
      total: 0,
      list: []
    },
    currrentClickIndex: null,
    currentShopList: []
  },
  onLoad() {
    this.queryData({
      status: 1,
      search: this.data.search,
      pageSize: this.data.pageSize,
      pageNumber: 1
    }).then(() => {
      this.queryData({
        status: 2,
        search: this.data.search,
        pageNumber: 1,
        pageSize: this.data.pageSize
      })
    })
  },
  // 处理下拉刷新函数
  onPullDownRefresh() {
    this.queryData({
      status: this.data.status,
      search: this.data.search,
      pageNumber: 1,
      pageSize: this.data.pageSize
    }).then(() => {
      dd.stopPullDownRefresh(); //停止下拉刷新
    })
  },
  // 页面被拉到底部
  onReachBottom() {
    if (this.data.status === 1 && this.data.private.total !== this.data.private.list.length) {
      this.queryData({
        status: this.data.status,
        search: this.data.search,
        pageSize: this.data.pageSize,
        pageNumber: this.data.private.list.length / this.data.pageSize + 1
      })
    } else if (this.data.status === 2 && this.data.signed.total !== this.data.signed.list.length) {
      this.queryData({
        status: this.data.status,
        search: this.data.search,
        pageSize: this.data.pageSize,
        pageNumber: this.data.signed.list.length / this.data.pageSize + 1
      })
    }
  },
  onSearch(e) {
    this.setData({
      search: e.detail.value
    })
    this.queryData({
      status: 1,
      search: this.data.search,
      pageSize: this.data.pageSize,
      pageNumber: 1
    }).then(() => {
      this.queryData({
        status: 2,
        search: this.data.search,
        pageSize: this.data.pageSize,
        pageNumber: 1
      })
    })
  },
  onTabSelect(e) {
    this.setData({
      status: e.target.dataset.status
    })
  },
  onMerchantSelect(e) {
    this.setData({
      currrentClickIndex: e.target.dataset.index
    })
    const key = this.data.status === 1 ? 'private' : 'signed'
    this.setData({
      currentShopList: this.data[key].list[this.data.currrentClickIndex].shopList
    })
  },
  onShopPickerClose() {
    this.setData({
      currrentClickIndex: null,
      currentShopList: []
    })
  },
  onShopPickerChange(shop) {
    debugger
  },
  queryData(params) {
    const key = params.status === 1 ? "private" : "signed"
    if (params.pageNumber === 1) {
      let obj = {}
      obj[key] = {
        total: 0,
        list: []
      }
      this.setData(obj)
    }
    return new Promise((resolve) => {
      dd.showLoading({
        content: "加载中..."
      })
      request({
        url: getMerchantList,
        data: params
      }).then(res => {
        let obj = {}
        if (params.pageNumber === 1) {
          obj[key] = {
            total: res.totalNum,
            list: res.voList
          }
          this.setData(obj)
        } else {
          this.data[key].list.push(...res.voList)
          obj[key] = this.data[key]
          this.setData(obj)
        }
      }).finally(() => {
        resolve()
        dd.hideLoading();
      })
    })
  }
})
